#define _CRT_SECURE_NO_WARNINGS 1
#include"sort.h"

void Swap(int* p, int* q)
{
	int tmp = *p;
	*p = *q;
	*q = tmp;
}

void InsertSort(int* a, int n)
{
	for (int i = 0; i < n - 1; i++)
	{
		int end = i;
		int tmp = a[end +1];
		while (end >= 0)
		{
			if (tmp < a[end])
			{
				a[end +1] = a[end];
				end--;
			}
			else
				break;
		}
		a[end + 1] = tmp;
	}
}

void selectsort(int* a,int left,int right)
{
	int begin = left;
	int end = right-1;
	while (begin<end)
	{
		int max = begin, min = begin;
		for (int i=begin+1; i <= end; i++)
		{
			if (a[max] < a[i])
				max = i;
			if (a[min] > a[i])
				min = i;
		}
		Swap(&a[begin], &a[min]);
		if (begin == max)
			max = min;
		Swap(&a[end], &a[max]);
		++begin;
		--end;
	}
}

int ThreeNum( int* a, int left, int right)
{
	int mid = left + (right - left) >> 1;
	if (a[left] > a[right])
	{
		if (a[left] < a[mid])
			return left;
		else if (a[mid] < a[right])
			return right;
		else
			return mid;
	}
	else
	{
		if (a[left] > a[mid])
			return left;
		else if (a[mid] > a[right])
			return right;
		else
			return mid;
	}
}

void quicksort(int* a, int left, int right)
{
	if (left >= right)
		return;

	if ((right-left+1) < 10)
		InsertSort(a+left,right -left+1);

	else
	{
		int key = quicksortpart(a, left, right);
		quicksort(a, left, key - 1);
		quicksort(a, key + 1, right);
		/*int mid = ThreeNum(a, left, right);
		Swap(&a[mid], &a[left]);

		int key = left;
		int begin = left;
		int end = right;
		while (begin < end)
		{

			while (begin < end && a[end] >= a[key])
				end--;
			while (begin < end && a[begin] <= a[key])
				begin++;

			Swap(&a[begin], &a[end]);
		}
		Swap(&a[key], &a[begin]);
		key = begin;*/
	}
}

void BubbleSort(int* a, int n)
{
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = 0; j < n - i - 1; j++)
		{
			if(a[j]>a[j+1])
				Swap(&a[j], &a[j + 1]);
		}
	}
}

//void AdjustDown(int* a, int n, int parent)
//{
//	int child = parent * 2 + 1;
//	while (child < n)
//	{
//		if ((child+1<n)&&a[child] < a[child + 1])
//			child++;
//		if (a[child] > a[parent])
//		{
//			Swap(&a[child], &a[parent]);
//			parent = child;
//			child = parent * 2 + 1;
//		}
//		else
//			break;
//	}
//}

//void HeapSort(int* a, int n)
//{
//	for (int i = (n - 1 - 1) / 2; i >= 0; i--)
//	{
//		AdjustDown(a, n, i);
//	}
//	int end = n - 1;
//	while(end>0)
//	{
//		Swap(&a[end], &a[0]);
//		AdjustDown(a, end, 0);
//		--end;
//	}
//}


void AdjustDown(int* a, int n, int parent)
{
	int child = parent * 2 + 1;
	while (child < n)
	{
		if ((child+1<n)&&a[child] < a[child + 1])
			child++;
		if (a[child] > a[parent])
		{
			Swap(&a[child], &a[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
			break;
	}
}

void HeapSort(int* a, int n)
{
	for (int i = (n - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDown(a, n, i);
	}
	int end = n - 1;
	while (end > 0)
	{
		Swap(&a[0], &a[end]);
		AdjustDown(a, end, 0);
		--end;
	}
}

int quicksortpart(int* a, int left, int right)
{
	int mid = ThreeNum(a, left, right);
	Swap(&a[mid], &a[left]);
	int key = left;
	int prev = left;
	int cur = prev+1;
	while (cur <= right)
	{
		if (a[cur] < a[key]&&++prev!=cur)
		{
			Swap(&a[prev], &a[cur]);
		}
		cur++;
	}
	Swap(&a[prev], &a[left]);
	return prev;
}

void quicksortnor(int* a, int left, int right)
{
	Stack st;
	StackInit(&st);
	StackPush(&st, right);
	StackPush(&st, left);
	while (!StackEmpty(&st))
	{
		int begin = StackTop(&st);
		StackPop(&st);
		int end = StackTop(&st);
		StackPop(&st);

		int key = quicksortpart(a, begin, end);

		if (key + 1 < end)
		{
			StackPush(&st, end);
			StackPush(&st, key + 1);
		}
		if (begin < key - 1)
		{
			StackPush(&st, key-1);
			StackPush(&st, begin);
		}
	}
}

void _mergesort(int* a,int* tmp, int begin, int end)
{
	if (begin == end)
		return;
	int mid = begin + (end - begin) / 2;
	_mergesort(a,tmp, begin, mid);
	_mergesort(a, tmp, mid+1, end);

	int begin1 = begin,begin2=mid+1;
	int end1 = mid,end2=end;
	int i = begin;
	while (begin1 <= end1 && begin2 <= end2)
	{
		if (a[begin1] < a[begin2])
		{
			tmp[i++] = a[begin1++];
		}
		if (a[begin1] > a[begin2])
			tmp[i++] = a[begin2++];
	}

	while (begin1 <= end1)
		tmp[i++] = a[begin1++];
	while (begin2 <= end2)
		tmp[i++] = a[begin2++];

	memcpy(a+begin, tmp+begin,(end-begin+1)*sizeof(int));
}

void mergesort(int* a,int left,int n)
{
	int* tmp = (int*)malloc(n*sizeof(int));
	if (tmp == NULL)
	{
		perror("malloc fail");

		exit(1);
	}
	//_mergesort(a, tmp,left, right);
	int gap = 1;
	while (gap < n)
	{
		for (int i = 0; i < n; i += 2 * gap)
		{
			int begin1 = i, begin2 = i + gap;
			int end1 = i +gap-1, end2 = i + 2 * gap - 1;
			int j = i;
			if (begin2 >= n)
				break;
			if (end2 >= n)
				end2 = n;

			while (begin1 <= end1 && begin2 <= end2)
			{
				if (a[begin1] < a[begin2])
				{
					tmp[j++] = a[begin1++];
				}
				if (a[begin1] > a[begin2])
					tmp[j++] = a[begin2++];
			}

			while (begin1 <= end1)
				tmp[j++] = a[begin1++];
			while (begin2 <= end2)
				tmp[j++] = a[begin2++];
			memcpy(a + i, tmp + i, (end2 - i + 1) * sizeof(int));
		}
		gap *= 2;
	}
	
}